#include <iostream>
#include <fstream>
#include <vector>

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>

#include <sstream>
#include <string>

#include "PalabraClass.cpp"
#include <cmath>

using namespace std;

float calcularDistancia(int doc1, int doc2, vector<string> archivos){
	
	Palabra palabraAux;
	vector<Palabra> documento1;
	vector<Palabra> documento2;
	
	std::ifstream ine(archivos[doc1],std::ios_base::binary);
	if(ine.good()){
		while (!ine.eof()){
			ine.read(palabraAux.palabra, sizeof(char[6]));
			//std::cout << "Reading char[6]: " << std::fixed << palabraAux.palabra << std::endl;
			ine.read((char*)&(palabraAux.frec), sizeof(float));
			//std::cout << "Reading floating point number: " << std::fixed << palabraAux.frec << std::endl;
			
			documento1.push_back(palabraAux);
			
		}
		ine.close();
	}
	
	std::ifstream ine2(archivos[doc2],std::ios_base::binary);
	if(ine2.good()){
		
		while (!ine2.eof()){
	  
			ine2.read(palabraAux.palabra, sizeof(char[6]));
			//std::cout << "Reading char[6]: " << std::fixed << palabraAux.palabra << std::endl;
			ine2.read((char*)&(palabraAux.frec), sizeof(float));
			//std::cout << "Reading floating point number: " << std::fixed << palabraAux.frec << std::endl;
			
			documento2.push_back(palabraAux);
			
		}
		ine2.close();
	}


	unsigned int contDoc1=0, contDoc2=0;
	
	float distanciaTotal = 0;

	while(contDoc1 < documento1.size() || contDoc2 < documento2.size()){
		
		if (contDoc1 == documento1.size()){
		
			while(contDoc2 < documento2.size()){
			
				distanciaTotal += (documento2[contDoc2].frec * documento2[contDoc2].frec);
				
				contDoc2++;
			
			}
		
		} 
		else {
			if (contDoc2 == documento2.size()){
			
				while(contDoc1 < documento1.size()){
				
					distanciaTotal += (documento1[contDoc1].frec * documento1[contDoc1].frec);
					
					contDoc1++;
				
				}
			
			}
			
			else {
				
				int comparacion = strcmp(documento1[contDoc1].palabra,documento2[contDoc2].palabra);
				
				if ( comparacion == 0 ){
				
					distanciaTotal+= pow((documento1[contDoc1].frec - documento2[contDoc2].frec),2);
					
					contDoc1++;
					contDoc2++;

				}
				
				else {
					
					if (comparacion < 0){
			
						distanciaTotal += (documento1[contDoc1].frec * documento1[contDoc1].frec);
						
						contDoc1++;
						
					}
					
					else {
						
						distanciaTotal += (documento2[contDoc2].frec * documento2[contDoc2].frec);
						
						contDoc2++;
					
					}
				}
			}
		}
	}

	distanciaTotal = sqrt(distanciaTotal);
	return distanciaTotal;
}

int main(){
	
	vector<string> documentos;
	
	ostringstream direccion;
	
	
	for (int i=0; i< 48; i++){
		
		direccion << "/tmp/docsParseados/Documento_" << i;
		documentos.push_back(direccion.str());
		direccion.str("");
		
	}
	
	for (int i=0; i<documentos.size(); i++){
		for(int j=0; j<documentos.size(); j++){
	
			cout << "calculo "  << i << "vs " << j << " = " << (calcularDistancia(i, j, documentos))<< endl;
		}
	}
	return 0;
}

